<?php
if( file_exists(get_template_directory().'/options/options.php') )
    include_once(get_template_directory().'/options/options.php');

if( file_exists(get_template_directory().'/theme-options.php') )
    include_once(get_template_directory().'/theme-options.php');

if (file_exists(dirname(__FILE__) . '/config.php')) {
	require_once ('config.php');
}


require_once ('vpn358_shotcode.php');
require_once ('vpn358_func/vpn358_widget.php');
/**
 * 获取PDF文件页数的函数获取
 * 文件应当对当前用户可读（linux下）
 * @param  [string] $path [文件路径]
 * @return [array]        [数组第一位表示成功与否，第二位表示提示信息]
 */
 function getPdfPages($path){
 	//ini_set("auto_detect_line_endings",true);
    if(!file_exists($path)) return array(false,"文件\"{$path}\"不存在！");
    if(!is_readable($path)) return array(false,"文件\"{$path}\"不可读！");
    // 打开文件
    $fp=@fopen($path,"r");
    if (!$fp) {
        return array(false,"打开文件\"{$path}\"失败");
    }else {
        $max=0;
        while(!feof($fp)) {
            $line = fgets($fp,255);
            if (preg_match('/\/Count [0-9]+/', $line, $matches)){
                preg_match('/[0-9]+/',$matches[0], $matches2);
                if ($max<$matches2[0]) $max=$matches2[0];
            }
        }
        fclose($fp);
        // 返回页数
        return array(true,$max);
    }
 }
//把pdf转换为pdf，在线阅读

function build_time_upload_filename($file) {
	$time = date("YmdHis", mktime() + 8 * 3600);
	$file['name'] = $time . mt_rand(10, 99) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
	//$file['name'] = iconv('GB2312', 'UTF-8', $file['name']);
	//$file['name'] = mb_convert_encoding( $file['name'],"UTF-8", "GBK");
	return $file;
}
add_filter('wp_handle_upload_prefilter', 'build_time_upload_filename');
//
function new_filename($filename, $filename_raw) {
	
    //global $post;
    //$info = pathinfo($filename);
    //$ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    //$new = $post->post_title . $ext;
    // the if is to make sure the script goes into an indefinate loop
    /*
    if( $new != $filename_raw ) {
        $new = sanitize_file_name( $new );
    }*/
    $new ='aa'.$filename_raw;
    return $new;
}
//add_filter('sanitize_file_name', 'new_filename',10,2);

//update_post_meta($attachment_ID, "image_rgb", $the_colour);
//pdf to flash
add_action("add_attachment", 'pdf_to_flash',99);
function pdf_to_flash($attachment_ID) {
/**
 **zhangyc tags
 ** convert pdf to swf files
 * update_post_meta($attachment_ID, "page_numbers", $the_colour);
 */
	$file = get_attached_file($attachment_ID);// Gets path to attachment file
	//echo "文件ID：".$attachment_ID;
	//update_post_meta($attachment_ID, "pages", "zhangyc");
	//echo $file;
	$file_name = basename($file);
	$file_dir = dirname($file);
	//$swf_file_name = bin2hex($file_name);
	$swf_file_name = $file_name;
	$swf_file_dir = $file_dir . '/pdf/';
//echo $file.'<br />swf_name:'.$swf_file_name.'<br /> swf_dir:'.$swf_file_dir.'<br />';
	$vpn358_pages=0;
	
	
	 $results=getPdfPages($file);
    if($results[0]){
        $vpn358_pages=$results[1];
        //echo "成功读取pdf文件页数：",$vpn358_pages;
    }else{
        // 在这里放置失败的处理代码
         $vpn358_pages=-1;
    }
    
	//wp_update_attachment_metadata($attachment_ID, $attach_data);
	add_post_meta($attachment_ID,"_vpn358_pages",$vpn358_pages);
	
	
	if (!is_dir($swf_file_dir)) {
		mkdir($swf_file_dir);
		//echo "create dir:".$swf_file_dir;
	}
	
	
	if (@file_exists($swf_file_dir . "/$filename" . ".swf")) {
		unlink($swf_file_dir . "/$filename" . ".swf");
	}
	
	$ext = pathinfo($file, PATHINFO_EXTENSION);

	if ($ext == 'pdf') {
		//
		if ($vpn358_pages > 0) {
			$cmd = "/usr/bin/pdf2swf -s flashversion=9 -s languagedir=/opt/www/xpdf/chinese-simplified -o " . $swf_file_dir . $swf_file_name . "%.swf " . $file;
		}else{
			$cmd = "/usr/bin/pdf2swf -s flashversion=9 -s languagedir=/opt/www/xpdf/chinese-simplified -o " . $swf_file_dir . $swf_file_name . ".swf " . $file;
		}
		pclose(popen($cmd, 'r'));
	}
	
	
}
///end pdf2swf
// adding it to the admin area
//add_filter('admin_footer_text', 'wp_bootstrap_custom_admin_footer');
require_once ('wp_bootstrap_navwalker.php');

/*
 *显示简单的文章标题
 *$category 分类ID
 *$count,篇数
 *$len_title,文章标题的字数
 *$show_link_url，true时将标题的url设置为link_url字段，false时将标题的url链接设置为文章
 *$icon , icon
 */
function cat_list_title_with_icon($category, $count, $len_title, $show_link_url,$icon) {
ob_start();
	?>
<?php query_posts("showposts=$count&cat=$category")?>
<div  class="panel panel-primary">
	<div class="panel-heading widgettitle">
		<i class="<?php echo $icon; ?>"></i><a class="panel-header" href="<?php echo get_category_link($category);?>">
<?php single_cat_title();?>
<div class="pull-right">
				<i class="icon-th"></i>&nbsp;&nbsp;
			</div>
		</a>
	</div>
	<ul class="menu">
<?php while (have_posts()):the_post();
		?>
	<li class="menu-item menu-item-type-custom menu-item-object-custom ">
	<?php
	$post_id = get_the_ID();
		$link_url = get_post_meta($post_id, 'link_url', $single = true);
		if (!$show_link_url || !$link_url) {
			$link_url = get_the_permalink();
		}
		?>
						<a href="<?php echo $link_url;?>" title="<?php the_title_attribute();?>">
	<?php
	echo mb_strimwidth(get_the_title(), 0, $len_title, "...");
		?>
	</a>
					</li>
	<?php
endwhile;?>
</ul>
</div>
<?php
$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
/*
 *显示简单的文章标题
 *$category 分类ID
 *$count,篇数
 *$len_title,文章标题的字数
 *$show_link_url，true时将标题的url设置为link_url字段，false时将标题的url链接设置为文章
 */
function cat_list_title($category, $count, $len_title, $show_link_url) {
	return cat_list_title_with_icon($category, $count, $len_title, $show_link_url,"icon-list-ul");
}
/*
 *带头条的文章
 *$category 分类ID
 *$len_desc,头条简介的字数
 *$count,篇数
 *$len_title,文章标题的字数
 *$len_content，头条新闻的简介字数
 */
function cat_list_top($category, $count, $len_title, $len_content) {
	ob_start();
	?>
<?php query_posts("showposts=$count&cat=$category")?>
<div  class="panel panel-default">
<?php
$i = 0;
	while (have_posts()):the_post();
		if ($i == 0) {
			?>
				<div class="headlinetop">
					<a href="<?php echo get_the_permalink();?>" target="_blank">
						<h3><?php the_title_attribute();?></h3>
						<p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', get_the_content())), 0, $len_content, "...");?>...</p>
					</a>
				</div>
	<?php } else {
			if ($i == 1) {echo '<ul class="menu">';
			}
			?>
	<li class="menu-item menu-item-type-custom menu-item-object-custom ">
	<?php $category = get_the_category(get_the_ID());
			echo '<a href="' . get_category_link($category[0]->term_id) . '">[' . $category[0]->cat_name . ']</a>';?>
						<a href="<?php echo get_the_permalink();?>" title="<?php the_title_attribute();?>">
	<?php
	echo mb_strimwidth(get_the_title(), 0, $len_title, "...");
			?>
	</a></li>
	<?php
	}
		$i++;
	endwhile;
	echo '</ul>';
	?>
</div>
<?php
$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
/*
 *显示简单的文章标题
 *$category 分类ID
 *$img,分类图片
 *$len_content,图片-标题-简介的简介的字数
 *$title,标题
 *$desc,分类简介
 *$len_title,仅列出文章标题的字数
 *$count,列表条数
 */
function cat_desc_list($category, $img, $title, $desc, $len_title, $count) {
	ob_start();
	?>
	<div  class="panel panel-default">
		<div class="panel-heading widgettitle">
			<a class="panel-header" href="<?php echo get_category_link($category);?>">
<?php echo get_the_category_by_ID($category)."  ";?>
				<div class="pull-right">
					<i class="icon-folder-open-alt"></i>
				</div>
			</a>
		</div>
		<div class="panel-body">
			<ul class="columnistrecom ">
				<li>
					<span class="author">
					<img src="<?php echo $img;?>"  alt="<?php echo $img_width . ":" . $img_height;?>">
					</span>
					<a class="summary" href="<?php echo get_category_link($category);?>" title="<?php echo $title;?>" target="_blank">
						<h4><?php echo $title;?></h4>
					</a>
					<p><?php echo $desc;?></p>
				</li>
			</ul>
			<ul class="ul-list">
<?php query_posts("showposts=$count&cat=$category");
	while (have_posts()):the_post();
		?>
							<li >
								<a href="<?php echo get_the_permalink();?>" title="<?php the_title_attribute();?>">
	<?php
	echo mb_strimwidth(get_the_title(), 0, $len_title, "...");
		?>
	</a>
							</li>
	<?php
endwhile;?>
</ul>
			</div><!-- paenl-body end -->
		</div>
<?php
$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
/*
 *显示简单的文章标题
 *$category 分类ID
 *$img_count,图片-标题-简介的内容篇数
 *$len_content,图片-标题-简介的简介的字数
 *$image_length_title,图片-标题-简介的标题字数
 *$img_width,图片-标题-简介的图片宽度
 *$img_height,图片-标题-简介的图片高度
 *$title_count,仅列出标题的篇数
 *$len_title,仅列出文章标题的字数
 */
function cat_img_list($category, $img_count, $len_content, $image_length_title, $img_width, $img_height, $title_count, $len_title) {
	ob_start();
	$count = $img_count + $title_count;
	?>
		<?php query_posts("showposts=$count&cat=$category")?>
		<div  class="panel panel-default">
			<div class="panel-heading widgettitle">
				<a class="panel-header" href="<?php echo get_category_link($category);?>">
<?php single_cat_title();?>;;
<div class="pull-right">
						<span  class="dashicons dashicons-portfolio"></span>
					</div>
				</a>
			</div>
<?php
$i = 0;
	while (have_posts()):the_post();
		if ($len_content > 0 and $i < $img_count) {
			$artical_img = catch_first_image(get_the_content());
			if (empty($artical_img)) {$artical_img = get_stylesheet_directory_uri() . "/images/cat5.jpg";
			}
			if ($i == 0) {echo '<ul class="columnistrecom">';
			}
			?>
							<li>
								<span class="author">
								<img src="<?php echo $artical_img;?>" width="<?php echo $img_width;?>" height="<?php echo $img_height;?>" alt="<?php echo $img_width . ":" . $img_height;?>">
								</span>
								<a class="summary" href="<?php the_permalink()?>" title="<?php the_title_attribute();?>" target="_blank">
									<h4><?php
	echo mb_strimwidth(get_the_title(), 0, $len_title - 4, "...");
			?></h4>
									<p><?php
	echo mb_strimwidth(strip_tags(apply_filters('the_content', get_the_content())), 0, $len_content, "...");
			?></p>
								</a>
							</li>
	<?php
	if ($i == $img_count - 1) {echo '</ul>';
			}
		} else {
			if ($i == $img_count) {echo '<ul class="menu">';
			}
			?>
							<li class="menu-item menu-item-type-custom menu-item-object-custom ">
								<a href="<?php echo get_the_permalink();?>" title="<?php the_title_attribute();?>">
	<?php
	echo mb_strimwidth(get_the_title(), 0, $len_title, "...");
			?>
	</a>
							</li>
	<?php
	if ($i == $count) {echo '</ul>';
			}
		}
		$i = $i + 1;
	endwhile;?>
</div>
<?php
$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
/*
 *2篇图片+列出标题
 *$category 分类ID
 *$img_count,图片-标题-简介的内容篇数
 *$len_content,图片-标题-简介的简介的字数
 *$image_length_title,图片-标题-简介的标题字数
 *$img_width,图片-标题-简介的图片宽度
 *$img_height,图片-标题-简介的图片高度
 *$title_count,仅列出标题的篇数
 *$len_title,仅列出文章标题的字数
 */
function cat_img2_list($category, $img_count, $len_content, $image_length_title, $img_width, $img_height, $title_count, $len_title) {
	ob_start();
	$count = $img_count + $title_count;
	?>
		<?php query_posts("showposts=$count&cat=$category")?>
		<div  class="panel panel-default">
			<div class="panel-heading widgettitle">
				<a class="panel-header" href="<?php echo get_category_link($category);?>">
<?php single_cat_title();?>
<div class="pull-right">
						<span  class="dashicons dashicons-portfolio"></span>
					</div>
				</a>
			</div>
<?php
$i = 0;
	while (have_posts()):the_post();
		if ($len_content > 0 and $i < $img_count) {
			$artical_img = catch_first_image(get_the_content());
			if (empty($artical_img)) {$artical_img = get_stylesheet_directory_uri() . "/images/cat5.jpg";
			}
			if ($i == 0) {echo '<ul class="articletop">';
			}
			?>
							<li>
								<a href="<?php the_permalink()?>" title="<?php the_title_attribute();?> target="_blank">
									<img src="<?php echo $artical_img;?>" width="120" height="90" alt="">
									<span class="caption"><?php the_title_attribute();?></span>
								</a>
							</li>
	<?php
	if ($i == $img_count - 1) {echo '</ul>';
			}
		} else {
			if ($i == $img_count) {echo '<ul class="menu">';
			}
			?>
							<li class="menu-item menu-item-type-custom menu-item-object-custom ">
								<a href="<?php echo get_the_permalink();?>" title="<?php the_title_attribute();?>">
	<?php
	echo mb_strimwidth(get_the_title(), 0, $len_title, "...");
			?>
	</a></li>
	<?php
	if ($i == $count) {echo '</ul>';
			}
		}
		$i = $i + 1;
	endwhile;?>
</div>
<?php
$content = ob_get_contents();
	ob_end_clean();
	return $content;
}
//得到缩略图
function catch_first_image($content) {
	$first_img = '';
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	$first_img = @$matches[1][0];
	if (empty($first_img)) {//Defines a default image
		return '';
	}
	return $first_img;
}
function register_vpn358_menus() {
	register_nav_menus(
		array('primary_menu' => __('主菜单'))
	);
}
add_action('init', 'register_vpn358_menus');
function book_tab2acc($category, $count, $id, $show_desc = false) {
	ob_start();
	$len_title = 6;
	$len_content = 135;
	query_posts("showposts=$count&cat=$category");
	$i = 0;
	$book_imgs = array();
	while (have_posts()):the_post();
		$content = get_the_content();
		$txt = strip_tags($content);
		if (mb_strlen($txt) > $len_content) {
			$txt = mb_substr($txt, 0, $len_content);
			$txt = $txt . "...";
		}
		$image = addslashes(catch_first_image($content));
		$book_imgs[$i] = array("title" => get_the_title(),
			"img" => $image,
			"link" => get_the_permalink(),
			"txt" => $txt);
		$i++;
	endwhile;
	//var_export($book_imgs);
	?>
			<div id="<?php echo 'tab2acc-' . $id;?>">
				<ul class="resp-tabs-list">
<?php
$ac = 0;
	foreach ($book_imgs as $key => $book) {
		if ($ac == 0) {
			$active = "active";
			$ac = 1;
		} else {
			$active = "";
		}
		$title = $book['title'];
		if (mb_strlen($title) > $len_title) {
			$title = mb_substr($title, 0, $len_title) . "...";
		}
		echo '<li>' . $title . '</li>';
	}
	?>
</ul>
				<div class="resp-tabs-container">
<?php
$ac = 0;
	foreach ($book_imgs as $key => $book) {
		if ($ac == 0) {
			$active = "active";
			$ac = 1;
		} else {
			$active = "";
		}
		$title = $book['title'];
		if (mb_strlen($title) > $len_title) {
			//$title = mb_substr($title, 0,$len_title);
		}
		$image = '<div class="row"><div class="col-xs-12 col-md-6"><a target="_blank" href="' . $book["link"] . '"><img height="250px" src="' . stripcslashes($book["img"]) . '"></a></div>';
		$content = $book["txt"];
		echo '<div>' . $image . '<div class="col-xs-12 col-md-6"><a target="_blank" href="' . $book["link"] . '"><h3>' . $title . '</h3></a><p>' . $content . '</p></div></div></div>';
	}
	?>
				</div>
			</div>
			<script type="text/javascript">
			jQuery('#<?php echo 'tab2acc-' . $id;?>').easyResponsiveTabs({type: 'vertical',fit: true});
			</script>
<?php
$flush_content = ob_get_contents();
	ob_end_clean();
	return $flush_content;
}
function book_carousel($category, $count, $id, $show_desc = false) {
	query_posts("showposts=$count&cat=$category");
	$i = 0;
	$book_imgs = array();
	while (have_posts()):the_post();
		$content = get_the_content();
		$txt = mb_substr(strip_tags($content), 0, 300);
		$image = addslashes(catch_first_image($content));
		$book_imgs[$i] = array("title" => get_the_title(),
			"img" => $image,
			"link" => get_the_permalink(),
			"txt" => $txt);
		$i++;
	endwhile;
	//var_export($book_imgs);
	?>
			<div id="carousel-book-<?php echo $id;?>" style="height:370px;" class="carousel slide" data-ride="carousel">
				<ol class="carousel-indicators">
<?php
$ac = 0;
	foreach ($book_imgs as $key => $book) {
		if ($ac == 0) {
			$active = "active";
			$ac = 1;
		} else {
			$active = "";
		}
		echo '<li data-target="#carousel-book-' . $id . '" data-slide-to="' . $key . '" class="' . $active . '"></li>';
	}
	?>
</ol>
				<div class="carousel-inner">
<?php
$ac = 0;
	foreach ($book_imgs as $key => $book) {
		if ($ac == 0) {
			$active = "active";
			$ac++;
		} else {
			$active = "";
		}
		?>
					<div class="item <?php echo $active;?>">
						<div class="row">
							<div class="col-xs-6 col-md-4 ">
								<a href="<?php echo $book['link'];?>">
									<img class=""
									style="margin-top:10px;"
									data-src="holder.js/350x350/auto/#777:#555/text:<?php echo $book['title'];?> "
									alt="<?php echo $book['title'];?>"
									src="<?php echo $book['img'];?>" width="350px" height="350px">
								</a>
							</div>
<?php if ($show_desc == true) {?>
							<div class=".col-xs-12 .col-md-8">
								<div class="">
									<a href="<?php echo $book['link'];?>">
										<h3><?php echo $book['title'];?></h3>
									</a>
									<p><?php echo $book['txt'];?>...<br />
									<a href="<?php echo $book['link'];?>">(阅读更多)</a></p>
								</div>
							</div>
<?php };?>
</div>
					</div>
<?php
}
	?>
				</div>
				<a class="left carousel-control" href="#carousel-book-<?php echo $id;?>" data-slide="prev">
					<span class="glyphicon glyphicon-chevron-left"></span>
				</a>
				<a class="right carousel-control" href="#carousel-book-<?php echo $id;?>" data-slide="next">
					<span class="glyphicon glyphicon-chevron-right"></span>
				</a>
			</div>
<?php
}
function the_content_max_charlength($content,$charlength) {
	$excerpt = $content;
	$charlength++;

	if ( mb_strlen( $excerpt ) > $charlength ) {
		$subex = mb_substr( $excerpt, 0, $charlength - 5 );
		$exwords = explode( ' ', $subex );
		$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
		if ( $excut < 0 ) {
			echo mb_substr( $subex, 0, $excut );
		} else {
			echo $subex;
		}
		echo '[...]';
	} else {
		echo $excerpt;
	}
}
function register_button($buttons) {
	array_push($buttons, "|", "recentposts");
	return $buttons;
}
function add_plugin($plugin_array) {
	$plugin_array['recentposts'] = get_stylesheet_directory_uri() . '/vpn358_func/vpn358_short.js';
	return $plugin_array;
}
function my_recent_posts_button() {
	if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
		return;
	}
	if (get_user_option('rich_editing') == 'true') {
		add_filter('mce_external_plugins', 'add_plugin');
		add_filter('mce_buttons', 'register_button');
	}
}
add_action('init', 'my_recent_posts_button');
///
function vpn358_register_sidebars() {
	register_sidebar(array(
		'id' => 'sidebar1',
		'name' => 'Main Sidebar',
		'description' => 'Used on every page BUT the homepage page template.',
		'before_widget' => '<div id="%1$s" class="widget %2$s "><div class="panel panel-default">',
		'after_widget' => '</div></div>',
		'before_title' => '<div class="panel-heading widgettitle"><i class="icon-location-arrow"></i>&nbsp;',
		'after_title' => '</div>',
	));
	register_sidebar(array(
		'id' => 'sb_homepage',
		'name' => 'Homepage Sidebar',
		'description' => 'Used only on the homepage page template.',
		'before_widget' => '<div id="%1$s" class="widget %2$s panel-niit panel-default">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="widgettitle">',
		'after_title' => '</h4>',
	));
	register_sidebar(array(
		'id' => 'footer1',
		'name' => 'Footer 1',
		'before_widget' => '<div id="%1$s" class="widget span4 %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="widgettitle">',
		'after_title' => '</h4>',
	));
}
vpn358_register_sidebars();
//前台启用图标字体
add_action('wp_enqueue_scripts', 'vpn358_load_dashicons');
function vpn358_load_dashicons() {
	wp_enqueue_style('dashicons');
}
// enqueue styles
if (!function_exists("vpn358_theme_styles")) {
	function vpn358_theme_styles() {
		// This is the compiled css file from LESS - this means you compile the LESS file locally and put it in the appropriate directory if you want to make any changes to the master bootstrap.css.
		wp_register_style('Tabs2Accordion', get_stylesheet_directory_uri() . '/css/easy-responsive-tabs.css', array(), null, 'all');
		wp_enqueue_style('Tabs2Accordion');
		//wp_register_style('bbpress', get_stylesheet_directory_uri() . '/css/bbpress.min.css', array(), null, 'all');
		//wp_enqueue_style('bbpress');
	}
}
add_action('wp_enqueue_scripts', 'vpn358_theme_styles');
// enqueue javascript
if (!function_exists("vpn358_theme_js")) {
	function vpn358_theme_js() {
		wp_register_style('css', get_stylesheet_directory_uri() . '/style.css');
		wp_enqueue_style('css');
		wp_register_script('Tabs2Accordion', get_stylesheet_directory_uri() . '/js/easyResponsiveTabs.js', array('jquery'), null);
		wp_enqueue_script('Tabs2Accordion');
	}
}
add_action('wp_enqueue_scripts', 'vpn358_theme_js');
function vpn358_theme_zui_css() {
	wp_register_style('zui_css', get_stylesheet_directory_uri() . '/css/zui1.3/dist/css/zui.css',array(),null);
	wp_enqueue_style('zui_css');
}
add_action('wp_enqueue_scripts', 'vpn358_theme_zui_css');
function vpn358_theme_zui_theme() {
	wp_register_style('zui_theme', get_stylesheet_directory_uri() . '/css/zui1.3/dist/css/zui-theme.css',array(),null);
	//wp_enqueue_style('zui_theme');
}
add_action('wp_enqueue_scripts', 'vpn358_theme_zui_theme');
function vpn358_theme_zui_js() {
	wp_register_script('zui_js', get_stylesheet_directory_uri() . '/css/zui1.3/dist/js/zui.js', array('jquery'), null);
	wp_enqueue_script('zui_js');
	wp_register_script('flexpaper', get_stylesheet_directory_uri() . '/js/flexpaper.js',array(),null);
	wp_enqueue_script('flexpaper');
	wp_register_script('flexpaper_handlers', get_stylesheet_directory_uri() . '/js/flexpaper_handlers.js',array(),null);
	wp_enqueue_script('flexpaper_handlers');
}
add_action('wp_enqueue_scripts', 'vpn358_theme_zui_js');

function vpn358_admin_function() {
	add_theme_page('vpn358主题管理', 'vpn358主题设置', 'administrator', 'ashu_slug', 'vpn358_admin_display_function');
}
function vpn358_admin_display_function() {
	echo '<h1>vpn358主题设置页面</h1>
			<h4><a href="http: //www.vpn358.com">vpn358主题，
				作者:Zhangyc，
			网址:http: //www.vpn358.com/</a></h4><br />';
	
	
	echo '<table><tr><td>分类名称</td><td>分类ID</td><td>文章篇数</td></tr>';
	$categorys = get_terms('category', 'orderby=name');
	foreach ($categorys as $category) {
		$output = '<tr><td>' . $category->name . "</td><td><em>" . $category->term_id . '</em></td><td>' . $category->count . '</td></tr>';
		echo $output;
	}
	echo '</table>';
}
add_action('admin_menu', 'vpn358_admin_function');
add_filter('embed_oembed_discover', '__return_true');
wp_oembed_add_provider('#http://v.youku.com/v_show/id_(.*?).html#i', 'http://player.youku.com/embed', true);
// Menu output mod
/*
function get_post_category_id($post_ID) {
	global $wpdb;
	$sql = "SELECT `term_taxonomy_id` FROM $wpdb->term_relationships WHERE `object_id`='" . $post_ID . "';";
	$cat_id = $wpdb->get_results($sql);
	foreach ($cat_id as $catId) {
		$output = $catId->term_taxonomy_id;
	}
	$myCatId = intval($output);//这里就获得当前文章所属分类的分类ID
	$term = get_term($myCatId, 'taxonomy_name');//taxonomy_name为自己定义的或者默认的
	echo $term->name;//得到当前文章所属分类的分类名称
}
*/
//禁止非管理员用户编辑自己的个人资料
/*
if( !is_admin() ) {
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('edit-profile', 'user-actions');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

}
*/
//默认角色用户无法进入后台
if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
  $current_user = wp_get_current_user();
  if($current_user->roles[0] == get_option('default_role')) {
    wp_safe_redirect( home_url() );
    exit();
  }
}

// 注册成功后跳转到指定页面
function __my_registration_redirect() {
  // 这里设置的是跳转到首页，要换成其他页面
  // 可以将home_url()改成你指定的URL
  // 如 return 'http://www.baidu.com';
  return home_url();
}
//add_filter( 'registration_redirect', '__my_registration_redirect' );
//如果是要跳转到退出前所在的页面，可以使用以下代码：
//增加字体大小和样式
if ( ! function_exists( 'wpex_mce_buttons' ) ) {
	function wpex_mce_buttons( $buttons ) {
		array_unshift( $buttons, 'fontselect' ); // 添加字体选择
		array_unshift( $buttons, 'fontsizeselect' ); // 添加字体大小选择
		array_push( $buttons, 'styleselect' );
		return $buttons;
	}
}
add_filter( 'mce_buttons_2', 'wpex_mce_buttons' );

if ( !function_exists( 'get_avatar' ) ) :
	/**
	* Retrieve the avatar for a user who provided a user ID or email address.
	*
	* @since 2.5
	* @param int|string|object $id_or_email A user ID, email address, or comment object
	* @param int $size Size of the avatar image
	* @param string $default URL to a default image to use if no avatar is available
	* @param string $alt Alternate text to use in image tag. Defaults to blank
	* @return string tag for the user's avatar
	*/
	function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
	if ( ! get_option('show_avatars') )
	return false;
	if ( false === $alt)
	$safe_alt = '';
	else
	$safe_alt = esc_attr( $alt );
	if ( !is_numeric($size) )
	$size = '96';
	$default = includes_url('images/blank.gif');
	$avatar = "";
	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
	}
endif;
function coolwp_remove_open_sans_from_wp_core() {
	 wp_deregister_style( 'open-sans' );
	 wp_register_style( 'open-sans', false );
	 wp_enqueue_style('open-sans','');
}
add_action( 'init', 'coolwp_remove_open_sans_from_wp_core' );

/**
* Disable the emoji's
 */
function disable_emojis() {
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );    
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );  
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
    add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
/**
 * Filter function used to remove the tinymce emoji plugin.
 * 
 * @param    array  $plugins  
 * @return   array             Difference betwen the two arrays
 */
function disable_emojis_tinymce( $plugins ) {
	return array_diff( $plugins, array( 'wpemoji' ) );
}
function duoshuo_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.duoshuo.com",$avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'duoshuo_avatar', 10, 3 );
?>



